window.onload = function () {


    showClassInfo();
    showInfo(1);
}


function showClassInfo() {

    axios.get("/class/findAll").then(resp => {
        let info = resp.data;
        let str = "";
        str += `<option></option>`;
        info.forEach(n => {
            str += `<option value="${n.name}">${n.name}</option>`;
        });
        $("classText").innerHTML = str;

    })
}

function showInfo(pageNO) {

    axios.get("/student/findByItem", {
        params: {
            pageNO,
            name: $("nameText").value,
            className: $("classText").value,
            address: $("homeText").value
        }
    }).then(resp => {
        let info = resp.data;
        let str = "";
        info.list.forEach(n => {
            str += `<tr><td>${n.name}</td><td>${n.gender}</td><td>${n.classBean.name}</td><td>${n.roomBean.address}</td><td>${n.phone}</td>
<td><input type="button" value="学生退房" onclick="delStudent(${n.id})">
 <input type="button" value="学生换房"onclick="showUpdateDiv(${n.id})">
  <input type="button" value="详细信息"onclick="showFindStudentDiv(${n.id})"></td></tr>`;
        });
        $("data").innerHTML = str;
        let cutStr = "";
        for (let i = 1; i <= info.toalPage; i++) {
            cutStr += `<a href="Javascript:showInfo(${i})" style="margin-left: 25px;text-decoration: none;color: black">${i}</a>`;
        }
        $("cutA").innerHTML = cutStr;
    })
}

function delStudent(id) {
    axios.get("/student/del", {
        params: {
            id
        }
    }).then(resp => {
        let info = resp.data;
        if (info == "ok") {
            showInfo(1);
            showDiv('showInfoDiv');
            $("showInfo").style.color = "black";
            $("showInfo").innerHTML = "删除成功";
        }
    })
}

function showDiv(id) {
    $(id).style.visibility = "visible";
}

function closeDiv(id) {
    $(id).style.visibility = "hidden";
}

function showAddDiv() {
    showDiv('addStudentDiv');
    axios.get("/class/findAll").then(resp => {
        let info = resp.data;
        let str = "";
        info.forEach(n => {
            str += `<option value="${n.id}">${n.name}</option>`;
        })
        $("classSelect").innerHTML = str;
    });
}

function fileChange() {
    var reader = new FileReader();//定义文件读取流对象
    reader.readAsDataURL($("fileText").files[0]);
    reader.onload = function (ev) {
        $("faceImg").src = ev.target.result;
    }
}

/**
 * 按照性别查询可住房间
 * @param value  性别
 * @param id  放房间集合对象的id
 */
function findBygender(value, id) {
    axios.get("/room/findByGender", {
        params: {
            gender: value
        }
    }).then(resp => {
        let info = resp.data;
        let str = "";
        info.forEach(n => {
            str += `<option value="${n.id}">${n.address}</option>`;
        })
        $(id).innerHTML = str;
    })
}

/**
 * 添加学生
 * @returns {Promise<void>}
 */
async function addStudent() {
    var fromObj = new FormData();
    fromObj.append("uploadImg", $("fileText").files[0]);
    let config = {
        headers: {'Content-Type': 'multipart/form-data'}
    }
    var uploadFileName;
    await axios.post("/upload", fromObj, config).then(resp => {
        uploadFileName = resp.data;
    });

    axios.get("/student/add", {
        params: {
            name: $("studentName").value,
            gender: $("genderSelect").value,
            phone: $("phoneText").value,
            imSrc: uploadFileName,
            classId: $("classSelect").value,
            roomId: $("roomSelect").value
        }

    }).then(resp => {
        let info = resp.data;
        if (info == "ok") {
            showDiv('showInfoDiv');
            $("showInfo").style.color = "black";
            $("showInfo").innerHTML = "添加成功";
            closeDiv('addStudentDiv');
            showInfo(1);
        }
    })
}

function showUpdateDiv(id) {

    axios.get("/student/findById", {
        params: {
            id
        }
    }).then(resp => {
        let info = resp.data;
        findBygender(info.gender, 'upRoomSelect');
        $("studentIdText").value=info.id;
        $("upNameSpan").innerHTML = info.name;
        $("upGenderSpan").innerHTML = info.gender;
        $("upClassSpan").innerHTML = info.classBean.name;
        $("upPhoneSpan").innerHTML = info.phone;
        $("upImg").src = "/jpg/" + info.imSrc;
    })
    showDiv('updateStudentRoomDiv');
}

function updateStudent(){
    axios.get("/student/update",{
        params:{
            id: $("studentIdText").value,
            roomId:$("upRoomSelect").value
        }
    }).then(resp=>{
        if (resp.data=="ok"){
            showInfo(1);
         closeDiv('updateStudentRoomDiv');
        showDiv('showInfoDiv');
        $("showInfo").style.color = "black";
        $("showInfo").innerHTML = "修改成功";
    }
    })
}
function showFindStudentDiv(id){

    showDiv('findStudentDiv');
    axios.get("/student/findById",{
        params:{
            id
        }

    }).then(resp=>{
        let info=resp.data;
        $("fNameSpan").innerHTML = info.name;
        $("fGenderSpan").innerHTML = info.gender;
        $("fClassSpan").innerHTML = info.classBean.name;
        $("fRoomSpan").innerHTML=info.roomBean.address;
        $("fPhoneSpan").innerHTML = info.phone;
        $("fTimeSpan").innerHTML=info.time;
        $("fImg").src ="/jpg/"+ info.imSrc;
    });
}



